package ads.pin.pinmon.business;

import java.sql.SQLException;
import java.util.List;

import ads.pin.pinmon.dal.PlayerCreatureDAO;
import ads.pin.pinmon.dal.PlayerDAO;
import ads.pin.pinmon.entity.Player;
import ads.pin.pinmon.exception.BusinessException;

public class PlayerBO {

	/**
	 * 
	 * @param player
	 * @throws BusinessException
	 */
	public void savePlayer(Player player) throws BusinessException {
		PlayerDAO playerDAO = new PlayerDAO();
		PlayerCreatureDAO playerCreatureDAO = new PlayerCreatureDAO();
		try {
			playerDAO.save(player);
			player.setId(playerDAO.searchLastId().getId());

			playerCreatureDAO.savePlayerCreature(player);
		} catch (SQLException e) {
			e.printStackTrace();
			// TODO add error msg -> failed to insert into database!
			throw new BusinessException(null);

		}
	}

	/**
	 * 
	 * @param player
	 * @throws BusinessException
	 */
	public void updatePlayer(Player player) throws BusinessException {
		PlayerDAO playerDAO = new PlayerDAO();
		try {
			playerDAO.update(player);
		} catch (SQLException e) {
			e.printStackTrace();
			// TODO add error msg -> failed to insert into database!
			throw new BusinessException(null);

		}
	}

	/**
	 * 
	 * @param id
	 * @throws BusinessException
	 */
	public void deletePlayerById(int id) throws BusinessException {
		PlayerDAO playerDAO = new PlayerDAO();
		try {
			playerDAO.deleteById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			// TODO add error msg -> failed to insert into database!
			throw new BusinessException(null);

		}
	}

	/**
	 * 
	 * @throws BusinessException
	 */
	public void deleteAllPlayers() throws BusinessException {
		PlayerDAO playerDAO = new PlayerDAO();
		try {
			playerDAO.deleteAll();
		} catch (SQLException e) {
			e.printStackTrace();
			// TODO add error msg -> failed to insert into database!
			throw new BusinessException(null);

		}
	}

	/**
	 * 
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public Player searchPlayerById(int id) throws BusinessException {
		PlayerDAO playerDAO = new PlayerDAO();
		try {
			return playerDAO.searchById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			// TODO add error msg -> failed to insert into database!
			throw new BusinessException(null);

		}
	}

	/**
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public Player searchLastPlayer() throws BusinessException {
		PlayerDAO playerDAO = new PlayerDAO();
		try {
			return playerDAO.searchLastId();
		} catch (SQLException e) {
			e.printStackTrace();
			// TODO add error msg -> failed to insert into database!
			throw new BusinessException(null);

		}
	}

	/**
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public List<Player> searchAllPlayers() throws BusinessException {
		PlayerDAO playerDAO = new PlayerDAO();
		try {
			return playerDAO.searchAll();
		} catch (SQLException e) {
			e.printStackTrace();
			// TODO add error msg -> failed to insert into database!
			throw new BusinessException(null);

		}
	}
}
